/**
  ******************************************************************************
  * @file    usart.c
  * @brief   This file provides code for the configuration
  *          of the USART instances.
  ******************************************************************************
  * @attention
  *
  * <h2><center>&copy; Copyright (c) 2021 STMicroelectronics.
  * All rights reserved.</center></h2>
  *
  * This software component is licensed by ST under BSD 3-Clause license,
  * the "License"; You may not use this file except in compliance with the
  * License. You may obtain a copy of the License at:
  *                        opensource.org/licenses/BSD-3-Clause
  *
  ******************************************************************************
  */


#include "usart.h"



#include "stdlib.h"
#include "string.h"
#include "tim.h"
#include "main.h"
#include <stdint.h>
#include <stdarg.h>

extern unsigned char UART1_Rx_Buf[MAX_REC_LENGTH];
extern unsigned int  UART1_Rx_cnt ;
extern unsigned char UART1_temp[REC_LENGTH];

extern unsigned char UART2_Rx_Buf[MAX_REC_LENGTH];
extern unsigned int  UART2_Rx_cnt ;
extern unsigned char UART2_temp[REC_LENGTH];


extern unsigned char LPUART1_Rx_Buf[MAX_REC_LENGTH];             
extern unsigned int  LPUART1_Rx_cnt;                 
extern unsigned char LPUART1_temp[REC_LENGTH];





UART_HandleTypeDef hlpuart1;
UART_HandleTypeDef huart1;
UART_HandleTypeDef huart2;



void MX_LPUART1_UART_Init(void)
{

  hlpuart1.Instance = LPUART1;
  hlpuart1.Init.BaudRate = 115200;
  hlpuart1.Init.WordLength = UART_WORDLENGTH_8B;
  hlpuart1.Init.StopBits = UART_STOPBITS_1;
  hlpuart1.Init.Parity = UART_PARITY_NONE;
  hlpuart1.Init.Mode = UART_MODE_TX_RX;
  hlpuart1.Init.HwFlowCtl = UART_HWCONTROL_NONE;
  hlpuart1.Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE;
  hlpuart1.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT;
  if (HAL_UART_Init(&hlpuart1) != HAL_OK)
  {
    Error_Handler();
  }

}


void MX_USART1_UART_Init(void)
{

  huart1.Instance = USART1;
  huart1.Init.BaudRate = 9600;
  huart1.Init.WordLength = UART_WORDLENGTH_8B;
  huart1.Init.StopBits = UART_STOPBITS_1;
  huart1.Init.Parity = UART_PARITY_NONE;
  huart1.Init.Mode = UART_MODE_TX_RX;
  huart1.Init.HwFlowCtl = UART_HWCONTROL_NONE;
  huart1.Init.OverSampling = UART_OVERSAMPLING_16;
  huart1.Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE;
  huart1.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT;
  if (HAL_UART_Init(&huart1) != HAL_OK)
  {
    Error_Handler();
  }

}


void MX_USART2_UART_Init(void)
{

  huart2.Instance = USART2;
  huart2.Init.BaudRate = 115200; 
  huart2.Init.WordLength = UART_WORDLENGTH_8B;
  huart2.Init.StopBits = UART_STOPBITS_1;
  huart2.Init.Parity = UART_PARITY_NONE;
  huart2.Init.Mode = UART_MODE_TX_RX;
  huart2.Init.HwFlowCtl = UART_HWCONTROL_NONE;
  huart2.Init.OverSampling = UART_OVERSAMPLING_16;
  huart2.Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE;
  huart2.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT;
  if (HAL_UART_Init(&huart2) != HAL_OK)
  {
    Error_Handler();
  }

}

void HAL_UART_MspInit(UART_HandleTypeDef* uartHandle)
{

  GPIO_InitTypeDef GPIO_InitStruct = {0};
  if(uartHandle->Instance==LPUART1)
  {
  

  
    
    __HAL_RCC_LPUART1_CLK_ENABLE();

    __HAL_RCC_GPIOB_CLK_ENABLE();
    /**LPUART1 GPIO Configuration
    PB10     ------> LPUART1_TX
    PB11     ------> LPUART1_RX
    */
    GPIO_InitStruct.Pin = GPIO_PIN_10|GPIO_PIN_11;
    GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
    GPIO_InitStruct.Pull = GPIO_NOPULL;
    GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
    GPIO_InitStruct.Alternate = GPIO_AF4_LPUART1;
    HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);

    
    HAL_NVIC_SetPriority(LPUART1_IRQn, 0, 0);
    HAL_NVIC_EnableIRQ(LPUART1_IRQn);
		 
  

  
  }
  else if(uartHandle->Instance==USART1)
  {
  

  
    
    __HAL_RCC_USART1_CLK_ENABLE();

    __HAL_RCC_GPIOA_CLK_ENABLE();
    /**USART1 GPIO Configuration
    PA9     ------> USART1_TX
    PA10     ------> USART1_RX
    */
    GPIO_InitStruct.Pin = GPIO_PIN_9|GPIO_PIN_10;
    GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
    GPIO_InitStruct.Pull = GPIO_NOPULL;
    GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
    GPIO_InitStruct.Alternate = GPIO_AF4_USART1;
    HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);

    
    HAL_NVIC_SetPriority(USART1_IRQn, 1, 0);
    HAL_NVIC_EnableIRQ(USART1_IRQn);
  

  
  }
  else if(uartHandle->Instance==USART2)
  {
  

  
    
    __HAL_RCC_USART2_CLK_ENABLE();

    __HAL_RCC_GPIOA_CLK_ENABLE();
    /**USART2 GPIO Configuration
    PA2     ------> USART2_TX
    PA3     ------> USART2_RX
    */
    GPIO_InitStruct.Pin = GPIO_PIN_2|GPIO_PIN_3;
    GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
    GPIO_InitStruct.Pull = GPIO_NOPULL;
    GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
    GPIO_InitStruct.Alternate = GPIO_AF4_USART2;
    HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);

    
    HAL_NVIC_SetPriority(USART2_IRQn, 2, 0);
    HAL_NVIC_EnableIRQ(USART2_IRQn);
  

  
  }
}

void HAL_UART_MspDeInit(UART_HandleTypeDef* uartHandle)
{

  if(uartHandle->Instance==LPUART1)
  {
  

  
    
    __HAL_RCC_LPUART1_CLK_DISABLE();

    /**LPUART1 GPIO Configuration
    PB10     ------> LPUART1_TX
    PB11     ------> LPUART1_RX
    */
    HAL_GPIO_DeInit(GPIOB, GPIO_PIN_10|GPIO_PIN_11);

    
    HAL_NVIC_DisableIRQ(LPUART1_IRQn);
  

  
  }
  else if(uartHandle->Instance==USART1)
  {
  

  
    
    __HAL_RCC_USART1_CLK_DISABLE();

    /**USART1 GPIO Configuration
    PA9     ------> USART1_TX
    PA10     ------> USART1_RX
    */
    HAL_GPIO_DeInit(GPIOA, GPIO_PIN_9|GPIO_PIN_10);

    
    HAL_NVIC_DisableIRQ(USART1_IRQn);
  

  
  }
  else if(uartHandle->Instance==USART2)
  {
  

  
    
    __HAL_RCC_USART2_CLK_DISABLE();

    /**USART2 GPIO Configuration
    PA2     ------> USART2_TX
    PA3     ------> USART2_RX
    */
    HAL_GPIO_DeInit(GPIOA, GPIO_PIN_2|GPIO_PIN_3);

    
    HAL_NVIC_DisableIRQ(USART2_IRQn);
  

  
  }
}



void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
{
  if(huart->Instance==USART1)
  {
    UART1_Rx_Buf[UART1_Rx_cnt] = UART1_temp[0];
    UART1_Rx_cnt++;
		if(UART1_Rx_cnt>=MAX_REC_LENGTH)
			UART1_Rx_cnt = 0;
    HAL_UART_Receive_IT(&huart1,(uint8_t *)UART1_temp,REC_LENGTH);
  }
	
	else if(huart->Instance==USART2)
  {
    UART2_Rx_Buf[UART2_Rx_cnt] = UART2_temp[0];
    UART2_Rx_cnt++;
		if(UART2_Rx_cnt>=MAX_REC_LENGTH)
			UART2_Rx_cnt = 0;
    HAL_UART_Receive_IT(&huart2,(uint8_t *)UART2_temp,REC_LENGTH);
  }
	else if(huart->Instance==LPUART1)
  {
    LPUART1_Rx_Buf[LPUART1_Rx_cnt] = LPUART1_temp[0];
    LPUART1_Rx_cnt++;
		if(LPUART1_Rx_cnt>=MAX_REC_LENGTH)
			LPUART1_Rx_cnt = 0;
    HAL_UART_Receive_IT(&hlpuart1,(uint8_t *)LPUART1_temp,REC_LENGTH);
  }
	else
	{
		
	}
	
}

void Clc_uart_recvbuf(UART_HandleTypeDef *huart)
{
	uint16_t i;
	if(huart->Instance==USART1)
	{
		UART1_Rx_cnt = 0;
		for(i=0;i<MAX_REC_LENGTH;i++)
			UART1_Rx_Buf[i] = 0;
	}
	else if(huart->Instance==LPUART1)
	{
		LPUART1_Rx_cnt = 0;
		for(i=0;i<MAX_REC_LENGTH;i++)
			LPUART1_Rx_Buf[i] = 0;
	}
}



char string[255] = {0};
void SysPrintf(const char *fmt, ...)
{
	va_list ap;

	uint16_t L = 0, i = 0;
	va_start(ap, fmt);
	vsprintf(string, fmt, ap);
	va_end(ap);
	L = strlen(string);
	RS485_SentData(string,L);
/* 	while (L--)
	{
		User_UART_Sent(string[i++]);
	} */
}

void User_UART_Sent(uint8_t val)
{
	uint8_t i;
	IO_LOW; 
	delay_us(208);
	for(i = 0; i < 8; i++) 
	{
		if(val & 0x01)
			IO_HIGH;
		else
		IO_LOW;
		
		delay_us(208);
		
		val >>= 1;
	}
	IO_HIGH;
	delay_us(208);
}



void RS485_SentData(uint8_t *buf,uint16_t len)
{
	RS485_HIGH;
	HAL_UART_Transmit(&huart2,buf,len,1000);
	HAL_Delay(10);
	RS485_LOW;
}




